package com.aaa.service.impl;

import com.aaa.common.domain.RentInAssetInfo;
import com.aaa.common.domain.RentInContractCRUDInfo;
import com.aaa.common.domain.RentInContractInfo;
import com.aaa.common.domain.RenterInfo;
import com.aaa.common.result.Result;
import com.aaa.mapper.RentInAssetMapper;
import com.aaa.mapper.RentInContractMapper;
import com.aaa.mapper.RentInfoMapper;
import com.aaa.service.RentInContractCRUDService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: 宋文森
 * @Date: 2021/02/06/21:46
 * @Description:
 */
@Service
public class RentInContractCRUDServiceImpl extends ServiceImpl<RentInContractMapper,RentInContractInfo> implements RentInContractCRUDService {

    @Autowired
    private RentInContractMapper rentInContractMapper;
    @Autowired
    private RentInContractInfo rentInContractInfo;
    @Autowired
    private RenterInfo renterInfo;
    @Autowired
    private RentInfoMapper rentInfoMapper;
    @Autowired
    private RentInAssetMapper rentInAssetMapper;

    @Transactional
    public Result RentInContractAdd(RentInContractCRUDInfo rentInContractCRUDInfo) {

        /**
         * rent_in_contract_in 表的添加
         */
        rentInContractInfo.setContractId(rentInContractCRUDInfo.getContractId());
        rentInContractInfo.setAssetId(rentInContractCRUDInfo.getAssetId());
        rentInContractInfo.setContact(rentInContractCRUDInfo.getContact());
        rentInContractInfo.setPhone(rentInContractCRUDInfo.getPhone());
        rentInContractInfo.setContractBeginDate(rentInContractCRUDInfo.getContractBeginDate());
        rentInContractInfo.setContractEndDate(rentInContractCRUDInfo.getContractEndDate());
        rentInContractInfo.setLessee(rentInContractCRUDInfo.getLessee());
        rentInContractInfo.setLessor(rentInContractCRUDInfo.getLessor());
        rentInContractInfo.setAnnualRent(rentInContractCRUDInfo.getAnnualRent());
        rentInContractInfo.setAddress(rentInContractCRUDInfo.getAddress());
        rentInContractInfo.setAssetTypeId(rentInContractCRUDInfo.getAssetTypeId());
        rentInContractInfo.setContractStatusId(rentInContractCRUDInfo.getContractStatusId());
        int insert = rentInContractMapper.insert(rentInContractInfo);
        renterInfo.setContact(rentInContractCRUDInfo.getContact());
        renterInfo.setPhone(rentInContractCRUDInfo.getPhone());
        renterInfo.setName(rentInContractCRUDInfo.getLessee());
        int insert1 = rentInfoMapper.insert(renterInfo);
        if (insert>0&&insert1>0){
            return Result.success("租入合同添加成功!");
        }

        return Result.error("租入合同添加失败!");

    }

    /**
     * 查询资产的租赁地址
     * @param rentInContractCRUDInfo
     * @return
     */
    public Result selectAddress(RentInContractCRUDInfo rentInContractCRUDInfo) {
        QueryWrapper<RentInAssetInfo> rentInAssetInfoQueryWrapper = new QueryWrapper<RentInAssetInfo>();
        rentInAssetInfoQueryWrapper.eq("asset_id", rentInContractCRUDInfo.getAssetId());
        RentInAssetInfo rentInAssetInfo = rentInAssetMapper.selectOne(rentInAssetInfoQueryWrapper);
        return Result.success(rentInAssetInfo);
    }




    /**
     * 资产租入合同的修改操作  修改之前得根据id查询数据
     * @param rentInContractInfo
     * @return
     */
    public Result updateRentInContract(RentInContractInfo rentInContractInfo) {
        int i = rentInContractMapper.updateById(rentInContractInfo);
        if (i>0){
            return Result.success("资产租入合同修改成功");
        }
        return Result.error("资产租入合同修改失败");
    }

    /**
     * 修改操作前得查询操作  公共的方法
     * @param id
     * @return
     */
    public Result listRentInContractById(int id){
        RentInContractInfo rentInContractInfo = rentInContractMapper.listRentInContractById(id);
        if (rentInContractInfo!=null){
          return   Result.success(rentInContractInfo);
        }
        return Result.error("查询数据失败！");
    }

    /**
     * 资产租入合同的删除操作
     * @param id
     * @return
     */
    public Result deleteRentInContractById(int  id) {
        int i = rentInContractMapper.deleteById(id);
        if (i>0){
            return Result.success("资产租入合同删除成功");
        }
        return Result.error("资产租入合同删除失败");
    }
}
